import os

menu = {
    "0": {"name": "退出系统", "action": "quit_system"},
    "1": {"name": "新建名片", "action": "build"},
    "2": {"name": "显示全部", "action": "show_all"},
    "3": {"name": "查询名片", "action": "find_card"},
    "4": {"name": "关键词搜索", "action": "find_card_list"},
}

system_name = '名片管理系统'
system_version = 'Bate 0.0.1'
card_list = []


def tips():
    """
    登录时候的提示
    :return:
    """
    format_str_center = '{0:^50}'
    format_str_left = '{0:<1}'
    format_str_right = '{0:>1}'

    print(format_str_center.format("*" * 50))
    print(format_str_left.format("*"), end="")
    # print(format_str_center.format(f"欢迎使用【{system_name}】{system_version}"), end="")
    print(f"欢迎使用【{system_name}】{system_version}".center(38), end="")
    print(format_str_right.format("*"))
    print("*" * 50)
    for key in menu:
        print(f'{key}. {menu[key]["name"]}')
    print("*" * 50)


def quit_system():
    """
    退出系统
    :return:
    """
    print(f"欢迎再次使用【{system_name}】")
    exit()


def build():
    """
    添加名片
    :return:
    """

    name = input("请输入姓名：")
    qq = input("请输入QQ：")
    tel = input("请输入电话：")

    append = {"name": name, "qq": qq, "tel": tel}
    card_list.append(append)
    print(f"添加 {name} 成功")


def show_all():
    """
    显示所有名片
    :rtype: object
    """
    if len(card_list) > 0:
        print("姓名\t\tQQ\t\t电话\t\t")
        print("-" * 50)
        for card_dict in card_list:
            print(f"{card_dict['name']}\t\t{card_dict['qq']}\t\t{card_dict['tel']}\t\t")
    else:
        print("未找到名片")


def find_card_list():
    """
    关键词批量搜索
    :return:
    """
    keyword = input("请输入关键字")
    # 存储符合关键字的记录
    find_data = []

    for card_dict in card_list:
        if (str(card_dict)).find(keyword) >= 0:
            find_data.append(card_dict)
    else:
        print("搜索结果为空")

    # 清除变量
    del card_dict

    print("姓名\t\tQQ\t\t电话\t\t")
    print("-" * 50)
    for card_dict in find_data:
        print(f"{card_dict['name']}\t\t{card_dict['qq']}\t\t{card_dict['tel']}\t\t")


def find_card():
    """
    名字查找记录
    :rtype: object
    """
    name = input("请输入名字：")
    for card_dict in card_list:
        if card_dict["name"] == name:
            # 显示搜索结果
            print("姓名\t\tQQ\t\t电话\t\t")
            print("-" * 50)
            print(f"{card_dict['name']}\t\t{card_dict['qq']}\t\t{card_dict['tel']}\t\t")
            print("-" * 50)

            while True:
                print("请选择要执行的操作 "
                      "[1] 修改 [2] 删除 [0] 返回上级菜单 ")
                action_str = input('请选择操作功能：')

                if action_str in ["1", "2", "0"]:
                    print(f"您选择的操作是【{action_str}】")
                    break
                else:
                    print("您输入的不正确，请重新选择")

            if action_str == "1":
                edit(card_dict)
            elif action_str == "2":
                del_card(card_dict)

            break
    else:
        print("搜索结果为空")


def edit(card_dict):
    """
    编辑名片
    :param card_dict:
    :return:
    """
    print('不修改该项可直接输入【回车键】')
    card_dict["name"] = verification(card_dict["name"], "请输入姓名：")
    card_dict["qq"] = verification(card_dict["qq"], "请输入QQ：")
    card_dict["tel"] = verification(card_dict["tel"], "请输入电话：")


def del_card(find_dict):
    """
    删除
    :param find_dict: 存储名片的数组指针
    :return:
    """
    ret = card_list.remove(find_dict)
    print(ret)
    print(f"成功删除 {find_dict['name']}")


def verification(value, msg="请输入参数："):
    """
    验证输入规则
    :param value:
    :param msg:
    :return:
    """
    get_in = input(msg)
    if len(get_in) > 1:
        value = get_in
    return value
